package org.mozhu.mboot.core.web.resource;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebResourceConfigurerAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public StaticWebResourceConfigurer staticWebResourceConfigurer() {
        return new StaticWebResourceConfigurer();
    }

    @Bean
    @ConditionalOnMissingBean
    public ExternalWebResourceConfigurer externalWebResourceConfigurer() {
        return new ExternalWebResourceConfigurer();
    }

    @Bean
    @ConditionalOnMissingBean
    public ThemeWebResourceConfigurer themeWebResourceConfigurer() {
        return new ThemeWebResourceConfigurer();
    }

    @Bean
    @ConditionalOnBean(WebResourceConfigurer.class)
    public WebMvcConfigurer webResourceMvcConfigurer(List<WebResourceConfigurer> webResourceConfigurers) {

        return new WebMvcConfigurer() {

            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                for (WebResourceConfigurer webResourceConfigurer : webResourceConfigurers) {
                    webResourceConfigurer.addResourceHandler(registry);
                }
            }
        };
    }
}
